<?php

/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */

/**
 *
 * @author "Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>"
 *
 */
abstract class QueryObject
{

  const LEFT = 'LEFT JOIN';

  const RIGHT = 'RIGHT JOIN';

  const INNER = 'INNER JOIN';

  /**
   *
   * @var DAO
   */
  public $dao;

  /**
   *
   * @var mixed
   */
  protected $fields;

  /**
   *
   * @var string
   */
  protected $table;

  /**
   *
   * @var string
   */
  protected $where;

  /**
   *
   * @var array
   */
  protected $data;

  /**
   *
   * @var string
   */
  protected $orderBy;

  /**
   *
   * @var integer
   */
  protected $limit;

  /**
   *
   * @var integer
   */
  protected $offset;

  /**
   *
   * @var string
   */
  protected $groupBy;

  /**
   *
   * @var string
   */
  protected $having;

  /**
   *
   * @var mixed
   */
  protected $joins;

  /**
   *
   * @var string
   */
  protected $sql;

  /**
   * Constructor.
   *
   * @param DAO $dao
   * @return void
   */
  public function __construct(DAO $dao)
  {
    $this->dao = $dao;
  }

  /**
   *
   * @return DomainDefinition
   */
  public function getConfig()
  {
    return $this->dao->getConfig();
  }

  /**
   * Get the repository connection
   *
   * @return mixed
   */
  public function db()
  {
    return $this->dao->db();
  }

  /**
   *
   * @param mixed $fields
   * @return QueryObject
   */
  public function select($fields)
  {
    $this->sql = null;

    if (! is_array($fields) && func_num_args() > 1) {
      $this->fields = func_get_args();
    }
    else {
      $this->fields = $fields;
    }

    return $this;
  }

  /**
   *
   * @param $table
   * @return QueryObject
   */
  public function from($table)
  {
    $this->sql = null;

    if (! is_array($table) && func_num_args() > 1) {
      $this->table = func_get_args();
    }
    else {
      $this->table = $table;
    }

    return $this;
  }

  /**
   *
   * @param $where
   * @param $data
   * @return QueryObject
   */
  public function where($where)
  {
    $this->sql = null;
    $this->where = $where;
    return $this;
  }

  /**
   *
   * @param $orderBy
   * @return QueryObject
   */
  public function orderBy($orderBy)
  {
    $this->sql = null;

    if (! is_array($orderBy) && func_num_args() > 1) {
      $this->orderBy = func_get_args();
    }
    else {
      $this->orderBy = $orderBy;
    }

    return $this;
  }

  /**
   *
   * @param $groupBy
   * @return QueryObject
   */
  public function groupBy($groupBy)
  {
    $this->sql = null;

    if (! is_array($groupBy) && func_num_args() > 1) {
      $this->groupBy = func_get_args();
    }
    else {
      $this->groupBy = $groupBy;
    }

    return $this;
  }

  /**
   *
   * @param $having
   * @return QueryObject
   */
  public function having($having)
  {
    $this->sql = null;
    $this->having = $having;
    return $this;
  }

  /**
   *
   * @param $limit
   * @return QueryObject
   */
  public function limit($limit)
  {
    $this->limit = ( int ) $limit;
    return $this;
  }

  /**
   *
   * @param $offset
   * @return QueryObject
   */
  public function offset($offset)
  {
    $this->offset = ( int ) $offset;
    return $this;
  }

  /**
   *
   * @param $table
   * @param $condition
   * @param $direction
   * @return QueryObject
   */
  public function join($table, $condition, $direction = QueryObject::INNER)
  {
    $this->sql = null;

    if (! is_array($this->joins)) {
      $this->joins = array();
    }

    $this->joins[] = sprintf('%s %s ON %s', $direction, $table, $condition);

    return $this;
  }

  /**
   *
   * @param $sql
   * @return QueryObject
   */
  public function query($sql)
  {
    $this->fields = null;
    $this->table = null;
    $this->where = null;
    $this->data = null;
    $this->orderBy = null;
    $this->groupBy = null;
    $this->having = null;
    $this->joins = null;

    $this->sql = trim($sql);

    return $this;
  }

  /**
   *
   * @return string
   */
  public function getEntity($i = 0)
  {
    $table = $this->table;

    if (is_array($table)) {
      $table = $table[$i];
    }

    $table = trim($table);

    preg_match('/^[{`]*([a-z_]+)(:? +(:?AS)? +([a-z_]+))?[`}]*$/i', $table, $o);

    if (isset($o[2])) {
      $entity = $o[2];
    }
    elseif (isset($o[1])) {
      $entity = $o[1];
    }
    else {
      $entity = $table;
    }

    return $entity;
  }

}

?>